Object.defineProperty(exports, "__esModule", {
  value: true
});
var def_CaptureUtils = /* */function () {
  function _ctor() {
    babel_classCallCheck(this, _ctor);
  }
  return babel_createClass(_ctor, null, [{
    key: "_capture",
    value: function _capture(e, t) {
      var n = _ctor.getCamera();
      var i = n.node;
      var s = new cc.RenderTexture();
      s.initWithSize(t.width, t.height, cc.gfx.RB_FMT_S8);
      i.setPosition(t.x, t.y);
      i.parent = e;
      n.orthoSize = t.height / 2;
      n.targetTexture = s;
      n.render(e);
      n.targetTexture = null;
      i.parent = null;
      return s;
    }
  }, {
    key: "_getNodeInfo",
    value: function _getNodeInfo(e) {
      var t = _ctor._tmpInfo;
      var n = e.getWorldMatrix(_ctor._tmpMat4).getScale(_ctor._tmpVec3);
      t.scaleX = n.x;
      t.scaleY = n.y;
      t.scaleZ = n.z;
      if (e == cc.director.getScene()) {
        var _e5 = cc.view._visibleRect;
        t.anchorX = t.anchorY = 0;
        t.width = _e5.width;
        t.height = _e5.height;
      } else {
        t.anchorX = e.anchorX;
        t.anchorY = e.anchorY;
        t.width = e.width;
        t.height = e.height;
      }
      return t;
    }
  }, {
    key: "getCamera",
    value: function getCamera() {
      var e = _ctor._camera;
      if (!e) {
        (e = _ctor._camera = new cc.Node("CaptureUtils").addComponent(cc.Camera)).alignWithScreen = false;
        e.ortho = true;
        e.nearClip = 0;
      }
      return e;
    }
  }, {
    key: "getCanvas",
    value: function getCanvas() {
      return _ctor._canvas || (_ctor._canvas = document.createElement("canvas"));
    }
  }, {
    key: "capture",
    value: function capture(e, t) {
      var n;
      var i = new cc.SpriteFrame();
      undefined === e && (e = cc.Canvas.instance.node || cc.director.getScene());
      undefined === t && (t = {});
      n = _ctor._getNodeInfo(e);
      undefined === t.width && (t.width = e.width * n.scaleX);
      undefined === t.height && (t.height = e.height * n.scaleY);
      undefined === t.x && (t.x = (.5 - e.anchorX) * e.width);
      undefined === t.y && (t.y = (.5 - e.anchorY) * e.height);
      this._rect = t;
      this._tmpRenderTexture = _ctor._capture(e, t);
      i.setTexture(this._tmpRenderTexture);
      i.setFlipY(true);
      return i;
    }
  }, {
    key: "getrect",
    value: function getrect() {
      return this._rect;
    }
  }, {
    key: "getColor",
    value: function getColor(e, t) {
      var n;
      new cc.SpriteFrame();
      undefined === e && (e = cc.Canvas.instance.node || cc.director.getScene());
      undefined === t && (t = {});
      n = _ctor._getNodeInfo(e);
      undefined === t.width && (t.width = e.width * n.scaleX);
      undefined === t.height && (t.height = e.height * n.scaleY);
      undefined === t.x && (t.x = (.5 - e.anchorX) * e.width);
      undefined === t.y && (t.y = (.5 - e.anchorY) * e.height);
      var i = _ctor._capture(e, t);
      var s = i.width;
      var a = i.height;
      var r = i.readPixels();
      var l = a - 1;
      var c = 4 * s;
      console.log(r);
      var d = [];
      for (; l >= 0;) {
        for (var _e6 = 0; _e6 < s; _e6++) {
          var _t4 = l * c + 4 * _e6;
          var _n2 = {};
          _n2.r = r[_t4 + 0];
          _n2.g = r[_t4 + 1];
          _n2.b = r[_t4 + 2];
          _n2.a = r[_t4 + 3];
          d.push(_n2);
        }
        l--;
      }
      return d;
    }
  }, {
    key: "getColor2",
    value: function getColor2(e, t) {
      var n;
      new cc.SpriteFrame();
      undefined === e && (e = cc.Canvas.instance.node || cc.director.getScene());
      undefined === t && (t = {});
      n = _ctor._getNodeInfo(e);
      undefined === t.width && (t.width = e.width * n.scaleX);
      undefined === t.height && (t.height = e.height * n.scaleY);
      undefined === t.x && (t.x = (.5 - e.anchorX) * e.width);
      undefined === t.y && (t.y = (.5 - e.anchorY) * e.height);
      return _ctor._capture(e, t).readPixels();
    }
  }, {
    key: "toImgUrl",
    value: function toImgUrl(e) {
      var t;
      var n = (e = e || this._tmpRenderTexture).width;
      var i = e.height;
      if (cc.sys.isNative) {
        var _o8 = e.readPixels();
        var s = jsb.fileUtils.getWritablePath() + "tmpImg.png";
        jsb.saveImageData(_o8, n, i, s);
        t = s;
      } else {
        var _s2 = _ctor.getCanvas();
        var a = _s2.getContext("2d");
        var r = _s2.toTempFilePathSync;
        var l = e.readPixels();
        var c = 4 * n;
        var d = 0;
        _s2.width = n;
        for (_s2.height = i; d < i;) {
          var _e7 = i - 1 - d;
          var _t5 = a.createImageData(n, 1);
          var _o9 = _e7 * n * 4;
          for (var _n3 = 0; _n3 < c; _n3++) {
            _t5.data[_n3] = l[_o9 + _n3];
          }
          a.putImageData(_t5, 0, d++);
        }
        t = "function" == typeof r ? r.call(_s2, {}) : _s2.toDataURL("image/png");
        a.clearRect(0, 0, n, i);
      }
      return t;
    }
  }]);
}();
exports.default = def_CaptureUtils;
def_CaptureUtils._tmpMat4 = cc.mat4();
def_CaptureUtils._tmpVec3 = cc.v3();
def_CaptureUtils._tmpInfo = {};
def_CaptureUtils._tmpRenderTexture = null;
def_CaptureUtils._rect = {};